﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;

namespace cloud_sharing
{
    public partial class Login : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32
                SendMessage(
                                IntPtr hWnd,
                                int msg,
                                int wParam,
                                [MarshalAs(UnmanagedType.LPWStr)]string lParam
                            );

        private const int EM_SETCUEBANNER = 0x1501;

        public Login()
        {
            InitializeComponent();
            Boolean connectionStatus = checkConnection();
            if (!connectionStatus)
            {
                tbSignInEmail.Hide();
                tbSignInPassword.Hide();
                btLogin.Hide();
                lbSignUp.Hide();
                lbForgetPassword.Hide();

                lbNoInternetConnection1.Show();
                lbNoInternetConnection2.Show();
                btRetryConnect.Show();
            }
            //
            // Add PlaceHolder
            //
            SendMessage(tbSignInEmail.Handle, EM_SETCUEBANNER, 0, "Email");
            SendMessage(tbSignInPassword.Handle, EM_SETCUEBANNER, 0, "Password");
            SendMessage(tbSignUpEmail.Handle, EM_SETCUEBANNER, 0, "Email");
            SendMessage(tbSignUpPassWord.Handle, EM_SETCUEBANNER, 0, "Password");
            SendMessage(tbSignUpPasswordConfirm.Handle, EM_SETCUEBANNER, 0, "Confirm Password");
            SendMessage(tbSignUpFirstName.Handle, EM_SETCUEBANNER, 0, "First Name");
            SendMessage(tbSignUpLastName.Handle, EM_SETCUEBANNER, 0, "Last Name");
        }

        private Boolean checkConnection()
        {
            Boolean connectionStatus;
            try
            {
                Ping myPing = new Ping();
                String host = "google.com";
                byte[] buffer = new byte[32];
                int timeout = 1000;
                PingOptions pingOptions = new PingOptions();
                PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
                if (reply.Status == IPStatus.Success)
                {
                    connectionStatus = true;
                }
                else
                {
                    connectionStatus = false;
                }
            }
            catch (Exception)
            {
                connectionStatus = false;
            }
            return connectionStatus;
        }

        private void tbSignInTextChanged(object sender, EventArgs e)
        {
            String email = tbSignInEmail.Text;
            String password = tbSignInPassword.Text;
            if (email.Equals("") && password.Equals(""))
            {
                btLogin.Enabled = false;
            }
            else
            {
                btLogin.Enabled = true;
            }
            lbErrorInf.Text = "";
            lbErrorInf.Hide();
        }

        private void lbTermHover(object sender, EventArgs e)
        {
            Font myFont = new Font(lbTerm.Font, FontStyle.Underline);
            lbTerm.Font = myFont;
        }

        private void lbTermLeave(object sender, EventArgs e)
        {
            Font myFont = new Font(lbTerm.Font, FontStyle.Regular);
            lbTerm.Font = myFont;
        }

        private void lbSignUpHover (object sender, EventArgs e)
        {
            Font myFont = new Font(lbSignUp.Font,FontStyle.Underline);
            lbSignUp.Font = myFont;
        }

        private void lbSignUpLeave(object sender, EventArgs e)
        {
            Font myFont = new Font(lbSignUp.Font, FontStyle.Regular);
            lbSignUp.Font = myFont;
        }

        private void lbForgetPasswordHover(object sender, EventArgs e)
        {
            Font myFont = new Font(lbForgetPassword.Font, FontStyle.Underline);
            lbForgetPassword.Font = myFont;
        }

        private void lbForgetPasswordLeave(object sender, EventArgs e)
        {
            Font myFont = new Font(lbForgetPassword.Font, FontStyle.Regular);
            lbForgetPassword.Font = myFont;
        }

        private void lbSignUp_Click(object sender, EventArgs e)
        {

            tbSignUpEmail.Text = tbSignInEmail.Text;
            String lbSignUpValue = lbSignUp.Text;
            if (lbSignUpValue.Equals("Sign up"))
            {
                tbSignInEmail.Hide();
                tbSignInPassword.Hide();
                tbSignUpFirstName.Show();
                tbSignUpLastName.Show();
                tbSignUpEmail.Show();
                tbSignUpPassWord.Show();
                tbSignUpPasswordConfirm.Show();
                btSignUp.Show();
                btLogin.Hide();
                lbSignUp.Text = "Sign in";
                lbForgetPassword.Hide();
                cbAgreeTerm.Show();
                lbTerm.Show();
            }
            else
            {
                tbSignInEmail.Show();
                tbSignInPassword.Show();
                tbSignUpFirstName.Hide();
                tbSignUpLastName.Hide();
                tbSignUpEmail.Hide();
                tbSignUpPassWord.Hide();
                tbSignUpPasswordConfirm.Hide();
                btSignUp.Hide();
                btLogin.Show();
                lbSignUp.Text = "Sign up";
                lbForgetPassword.Show();
                cbAgreeTerm.Hide();
                lbTerm.Hide();
            }
        }

        private void lbForgetPassword_Click(object sender, EventArgs e)
        {

        }

        private void tbSignUpTextChanged(object sender, EventArgs e)
        {
            String firstName = tbSignUpFirstName.Text;
            String lastName = tbSignUpLastName.Text;
            String email = tbSignUpEmail.Text;
            String password = tbSignUpPassWord.Text;
            String passwordConfirm = tbSignUpPasswordConfirm.Text;
            if (firstName.Equals("") && lastName.Equals("") && email.Equals("") && password.Equals("") && passwordConfirm.Equals(""))
            {
                btSignUp.Enabled = false;
            }
            else
            {
                btSignUp.Enabled = true;
            }
            lbErrorInf.Text = "";
            lbErrorInf.Hide();
        }

        private void btLogin_Click(object sender, EventArgs e)
        {
            String email = tbSignInEmail.Text;
            String password = tbSignInPassword.Text;
            if (invalidEmail(email))
            {
                lbErrorInf.Text = "Invalid email";
                lbErrorInf.Show();
                return;
            }
            if (email.Equals(""))
            {
                lbErrorInf.Text = "Please input your email";
                lbErrorInf.Show();
                return;
            }
            else if (password.Equals(""))
            {
                lbErrorInf.Text = "Please input your password";
                lbErrorInf.Show();
                return;
            }
            Boolean connectionStatus = checkConnection();
            if (!connectionStatus)
            {
                lbNoInternetConnection1.Show();
                lbNoInternetConnection2.Show();
                btRetryConnect.Show();
                tbSignInEmail.Hide();
                tbSignInPassword.Hide();
                btLogin.Hide();
                lbSignUp.Hide();
                lbForgetPassword.Hide();
                return;
            }
            if (email.Equals("test@test.com") && password.Equals("123456"))
            {
                Form2 preference = new Form2();
                preference.Show();
                this.Hide();
            }
            else
            {
                lbErrorInf.Text = "Invalid email or password!";
                lbErrorInf.Show();
                return;
            }
        }

        private Boolean invalidEmail(String email) 
        {
            String[] formatEmail = email.Split('@');
            return (formatEmail.Length != 2);
            
        }

        private void btRetryConnect_Click(object sender, EventArgs e)
        {
            Boolean connectionStatus = checkConnection();
            if (connectionStatus)
            {
                lbNoInternetConnection1.Hide();
                lbNoInternetConnection2.Hide();
                btRetryConnect.Hide();
                if ((lbSignUp.Text).Equals("Sign up"))
                {
                    tbSignInEmail.Show();
                    tbSignInPassword.Show();
                    btLogin.Show();
                    lbSignUp.Show();
                    lbForgetPassword.Show();
                }
                else
                {
                    tbSignUpFirstName.Show();
                    tbSignUpLastName.Show();
                    tbSignUpEmail.Show();
                    tbSignUpPasswordConfirm.Show();
                    tbSignUpPassWord.Show();
                    cbAgreeTerm.Show();
                    lbTerm.Show();
                    lbSignUp.Show();
                    btSignUp.Show();
                }
            }
        }

        private void btSignUp_Click(object sender, EventArgs e)
        {
            String firstName = tbSignUpFirstName.Text;
            String lastName = tbSignUpLastName.Text;
            String email = tbSignUpEmail.Text;
            String password = tbSignUpPassWord.Text;
            String passwordConfirm = tbSignUpPasswordConfirm.Text;
            Boolean isAgreeWithTerm = cbAgreeTerm.Checked;
            if (invalidEmail(email))
            {
                lbErrorInf.Text = "Invalid email";
                lbErrorInf.Show();
                return;
            }
            if (firstName.Equals(""))
            {
                lbErrorInf.Text = "Please insert your first name";
                lbErrorInf.Show();
                return;
            }
            else if (lastName.Equals(""))
            {
                lbErrorInf.Text = "Please insert your last name";
                lbErrorInf.Show();
                return;
            }
            else if (email.Equals(""))
            {
                lbErrorInf.Text = "Please insert your email";
                lbErrorInf.Show();
                return;
            }
            else if (password.Equals(""))
            {
                lbErrorInf.Text = "Please insert your password";
                lbErrorInf.Show();
                return;
            }
            else if (passwordConfirm.Equals(""))
            {
                lbErrorInf.Text = "Please insert password confirm";
                lbErrorInf.Show();
                return;
            }
            else if (!isAgreeWithTerm)
            {
                lbErrorInf.Text = "Please read the Term and agree with it";
                lbErrorInf.Show();
                return;
            }
            else if (password != passwordConfirm)
            {
                lbErrorInf.Text = "Password and password confirm must be the same";
                lbErrorInf.Show();
                return;
            }
            Boolean connectionStatus = checkConnection();
            if (!connectionStatus)
            {
                lbNoInternetConnection1.Show();
                lbNoInternetConnection2.Show();
                btRetryConnect.Show();
                tbSignUpFirstName.Hide();
                tbSignUpLastName.Hide();
                tbSignUpPassWord.Hide();
                tbSignUpPasswordConfirm.Hide();
                tbSignUpEmail.Hide();
                cbAgreeTerm.Hide();
                lbTerm.Hide();
                btSignUp.Hide();
                lbSignUp.Hide();
                return;
            }
        }
    }
}
